<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentEditAlbum.class.php, $Author:  $ */



/** Component for edit album
 *
 * @version $Id: PhpFox_ComponentEditAlbum.class.php 297 2008-02-07 15:05:58Z phpfoxsv $
 * @package module.music
 */
class PhpFox_Mod_Music_ComponentEditAlbum extends BaseComponent
{
    function process()
    {
        $oAlbumItem    = &$this->_oModule->getItem('Album');
        /* @var $oMusicService PhpFox_Mod_Music_ItemAlbum */
        $oMusicService = &$this->_oModule->getService('Music');
        /* @var $oMusicService PhpFox_Mod_Music_ServiceMusic */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = &$this->_oModule->getTemplate();

        $oReq = &App::getRequest();
        $iAlbumId = $oReq->getInt('id');
        $oAlbumItem->load($iAlbumId);
        $oCurUser = App::loadUser();
        if(!$oSecurityService->isStaff() AND $oAlbumItem->aData['user_id']!=$oCurUser->aData['id'])
        {
        	App::gotoUrl('public.music');
        }
                

        $oSecurityService->checkPerm('music', array('full', 'read_only'));
        $oSecurityService->checkAccess('music');
        $picPath = App::getParam('music.album.pic.path').$iAlbumId.'.jpg';
        $bAlbumPicExist = (file_exists($picPath)) ? true: false;

        $sAction = $oReq->get('action');

        App::loadClass('Validator');
        $aFields = array
        (
            'val[musician_name]' => array('title' => App::format('Mod_Music.musician_name'), 'def' => 'required'),
            'val[album_title]'   => array('title' => App::format('Mod_Music.album_title'), 'def' => 'required'),
            'val[album_year]'    => array('title' => App::format('Mod_Music.album_year'), 'min' => 1901, 'max' => date('Y'), 'optional' => true),
            'val[album_description]' => array('title' => App::format('Mod_Music.description'),   'maxlen' => 3000,       'optional' => true),
        );
        $oValidator = &new Validator($aFields);

            if ( $oReq->get('edit') )
            {
                $oSecurityService->checkPerm('music', array('full'));
                $aValues = $oReq->getArray('val');
                if ('- - - -' == $aValues['album_year'])
                {
                    unset($aFields['val[album_year]']);
                }

                $oValidator = &new Validator($aFields);

                if (!$oValidator->isValid($oReq->getAll()))
                {
                    $this->addErrors($oValidator->getErrors());
                }
                if (!count($this->getErrors()))
                {
                    $iId = $oReq->getInt('id');
                    $aAlbumData = array(
                        'musician_name'     => htmlspecialchars($aValues['musician_name']),
                        'album_title'       => htmlspecialchars($aValues['album_title']),
                        'genre_id'          => $aValues['genre_sel'],
                        'album_year'        => htmlspecialchars($aValues['album_year']),
                        'album_description' => htmlspecialchars($aValues['album_description']),
                    );
                    if (!$oMusicService->saveAlbum($iAlbumId, $aAlbumData)){
                        $this->addErrors($oMusicService->getErrors());
                    }
                    if (isset($_FILES['album_image']) && $_FILES['album_image']['size'])
                    {
                        if (!$oMusicService->_uploadAlbumPic('album_image', $iAlbumId))
                        {

                        }
                    }

                    App::gotoUrl('account.myitems.editmusic.editalbum', array('id'=>$iAlbumId), App::format('global_updated'));
                }
			}

        switch ($sAction)
        {
            case 'delete_album':
                $oSecurityService->checkPerm('music', array('full'));
                if (!$oMusicService->deleteAlbum($iAlbumId)){
                    $this->addErrors($oMusicService->getErrors());
                    /* duplicate actions in deleteAlbum()
                    $aCond  = array('album_id'         => '='.$iId,
                                    );
                    list($aSongs,$iCnt) = $oMusicService->getSongsList($aCond);
                    foreach($aSongs as $val){
                        $iSongId = intval($val['song_id']);
                        $oMusicService->deleteSong($iSongId);
                    }*/
                }else{
	                App::gotoUrl('account.myitems.editmusic');
                }
                break;
            case 'delete_pic':
                $oSecurityService->checkPerm('music', array('full'));
                if ( $bAlbumPicExist ){
                    unlink($picPath);
                    $bAlbumPicExist = false;
                }
                break;
            default:
                $sAction = '';
                break;
        }

        if ($sAction){
            App::gotoUrl('account.myitems.editmusic.editalbum',array('id'=>$iAlbumId));
        }

        App::setBackUrl(App::getPageName());

        list($aAlbum,$iCount) = $oMusicService->getAlbumsList(array('album_id'=>'='.$iAlbumId));

        $oTpl->assignSrc(array(
            'sValidatorJs'   => $oValidator->makeJS(),
            'sValidatorMess' => $oValidator->makeJsMess(),
        ));

        $oTpl->assign(array(
            'aErrors'        => $this->getErrors(),
            'aAlbum'         => $aAlbum[0],
            'sAlbumImageUrl' => App::getParam('music.album.pic.url'),
            'aSongs'         => $oMusicService->getSongNames(array('album_id'=>'='.$iAlbumId)),
            'aGenres'        => $oMusicService->getGenres(),
            'sCurrentGenre'  => $aAlbum[0]['genre_id'],
            'bAlbumPic'      => $bAlbumPicExist,
        ));
        return $oTpl->fetch('EditAlbum.html');
    }
}
?>